<?php
/* ******************************
 *	Print aJax actions
 * ******************************
 */
include('../../includes/classes/core.php');
include('../../includes/classes/barcode/tfpdf.php');

$patch		= realpath(dirname(__FILE__) . '/../..') . '/media/fonts/unifont/DejaVuSansCondensed.ttf';
$patch1		= realpath(dirname(__FILE__) . '/../..') . '/media/fonts/unifont/DejaVuSansCondensed-Bold.ttf';

$img_patch	= realpath(dirname(__FILE__) . '/../..') . '/media/uploads/images/properties/logo.jpg';


$action 	= $_REQUEST['act'];
$user_id	= $_SESSION['USERID'];

$pdf		= new tFPDF();

switch ($action) {
	case 'get_barcode':
		$barcode_list = json_decode($_REQUEST['list']);
		$pdf->AddFont('DejaVu', '', $patch, true);
		$pdf->AddFont('DejaVub', '', $patch1, true);
		$pdf->SetFont('DejaVu', '', 89);

		for ($i = 0; $i < count($barcode_list); $i++) {

			$code 			= (string)GetBarCode($barcode_list[$i]);
			$array 			= array();
			$array 			= GetClientInfo($code);
			$client_image	= realpath(dirname(__FILE__) . '/../..') . '/media/uploads/images/client/'.$array['image'];

			$pdf->AddPage();


			$pdf->Cell( 0, 0, $pdf->Image($client_image, 2, 28, 23), 0, 9, 'L', false ); //client logo

			$pdf->SetFont('DejaVub', '', 30);
			$pdf->Cell(0, 0, $array['cartridge'], 0, 0, 'R');				//cartridge


			$pdf->SetXY(2,5);
			$pdf->SetFont('DejaVu', '', 17);
			$pdf->MultiCell( 120, 5, $array['name'], 0, 'L');


			$pdf->SetXY(0, 20);
			$pdf->Code128(29, 22, $array[code], 150, 35);							//code

			$pdf->SetXY(0, 60);
			$pdf->SetFont('DejaVu', '', 23);
			$pdf->write(5, $array[code]);

			$pdf->SetXY(85, 62);
			$pdf->SetFont('DejaVu', '', 23);
			$pdf->Cell(0, 0, 'Tel: 2 555 118 www.gsunity.ge', 0, 0, 'R');

			$pdf->SetXY(30, 71);
			$pdf->SetFont('DejaVu', '', 23);
			$pdf->Cell(0, 0, $array[prod].'  შპს "ჯი ეს იუნიტი"-ს მიერ', 0, 0, 'R');

			$pdf->SetXY(150, 1);
			$pdf->Cell( 0, 0, $pdf->Image($img_patch, 178, 28, 23), 0, 9, 'L', false );


		}

		$pdf->Output();

		break;
	case 'get_print_barcode':
		$barcode_list = $_REQUEST['list'];
		$pdf->AddFont('DejaVu', '', $patch, true);
		$pdf->AddFont('DejaVub', '', $patch1, true);
		$pdf->SetFont('DejaVu', '', 89);

		$code 			= $barcode_list;
		$array 			= array();
		$array 			= GetClientInfo($code);
		$client_image	= realpath(dirname(__FILE__) . '/../..') . '/media/uploads/images/client/'.$array['image'];

		$pdf->AddPage();


		$pdf->Cell( 0, 0, $pdf->Image($client_image, 2, 28, 23), 0, 9, 'L', false ); //client logo

		$pdf->SetFont('DejaVub', '', 30);
		$pdf->Cell(0, 0, $array['cartridge'], 0, 0, 'R');				//cartridge


		$pdf->SetXY(2,5);
		$pdf->SetFont('DejaVu', '', 17);
		$pdf->MultiCell( 120, 5, $array['name'], 0, 'L');


		$pdf->SetXY(0, 20);
		$pdf->Code128(29, 22, $array[code], 150, 35);							//code

		$pdf->SetXY(0, 60);
		$pdf->SetFont('DejaVu', '', 23);
		$pdf->write(5, $array[code]);

		$pdf->SetXY(85, 62);
		$pdf->SetFont('DejaVu', '', 23);
		$pdf->Cell(0, 0, 'Tel: 2 555 118 www.gsunity.ge', 0, 0, 'R');

		$pdf->SetXY(30, 71);
		$pdf->SetFont('DejaVu', '', 23);
		$pdf->Cell(0, 0, $array[prod].' შპს "ჯი ეს იუნიტი"-ს მიერ', 0, 0, 'R');

		$pdf->SetXY(150, 1);
		$pdf->Cell( 0, 0, $pdf->Image($img_patch, 178, 28, 23), 0, 9, 'L', false );

		$pdf->Output();

		break;
	case 'get_warehous_barcode' :
		$id = $_REQUEST['wdid'];
		$pdf->AddFont('DejaVu', '', $patch, true);
		$pdf->AddFont('DejaVub', '', $patch1, true);
		$pdf->SetFont('DejaVu', '', 89);

		$rResult = mysql_query("SELECT		`warehouse_detail`.`barcode`

								FROM		`warehouse_detail`
								WHERE		`warehouse_detail`.`id` = $id");
		$rResult = mysql_fetch_array( $rResult );
		$code = (string)$rResult[0];

		$array 			= array();
		$array 			= GetClientInfo($code);
		$client_image	= realpath(dirname(__FILE__) . '/../..') . '/media/uploads/images/client/'.$array['image'];

		$pdf->AddPage();


		$pdf->Cell( 0, 0, $pdf->Image($client_image, 2, 28, 23), 0, 9, 'L', false ); //client logo

		$pdf->SetFont('DejaVub', '', 30);
		$pdf->Cell(0, 0, $array['cartridge'], 0, 0, 'R');				//cartridge


		$pdf->SetXY(2,5);
		$pdf->SetFont('DejaVu', '', 17);
		$pdf->MultiCell( 120, 5, $array['name'], 0, 'L');


		$pdf->SetXY(0, 20);
		$pdf->Code128(29, 22, $code, 150, 35);							//code

		$pdf->SetXY(0, 60);
		$pdf->SetFont('DejaVu', '', 23);
		$pdf->write(5, $code);

		$pdf->SetXY(85, 62);
		$pdf->SetFont('DejaVu', '', 23);
		$pdf->Cell(0, 0, 'Tel: 2 555 118 www.gsunity.ge', 0, 0, 'R');

		$pdf->SetXY(30, 71);
		$pdf->SetFont('DejaVu', '', 23);
		$pdf->Cell(0, 0, $array[prod].' შპს "ჯი ეს იუნიტი"-ს მიერ', 0, 0, 'R');

		$pdf->SetXY(150, 1);
		$pdf->Cell( 0, 0, $pdf->Image($img_patch, 178, 28, 23), 0, 9, 'L', false );

		$pdf->Output();
		break;

	case 'full_pack' :
		$id = $_REQUEST['wid'];
		$pdf->AddFont('DejaVu', '', $patch, true);
		$pdf->AddFont('DejaVub', '', $patch1, true);
		$pdf->SetFont('DejaVu', '', 89);

		$rResult = mysql_query("SELECT			client.`name` AS `client`,
												client.image AS `image`,
												client_objects.`name` AS `object`,
												IF((!ISNULL(`a_letter`.`id`)),'კარტრიჯები აღდგენილია','პრინტერი შეკეთებულია' ) AS prod,
												IF((!ISNULL(`a_letter`.`id`)), (CONCAT('ბ – ',`a_letter`.`id`)),(CONCAT('ე – ',`d_letter`.`id`))) as latter,
												IF((!ISNULL(`a_letter`.`id`)),a_letter.fact_end_date,d_letter.fact_end_date ) AS job_end
						FROM			`warehouse`
						JOIN       client_objects ON client_objects.id = warehouse.client_object_id
						JOIN        	client ON client.id = client_objects.client_id
						LEFT JOIN `a_letter` ON `a_letter`.`id`  = `warehouse`.`a_letter_id`
						LEFT JOIN `d_letter` ON `d_letter`.`id`  = `warehouse`.`d_letter_id`
								WHERE			`warehouse`.`id` = $id");

		$rResult = mysql_fetch_array( $rResult );
		$client_image	= realpath(dirname(__FILE__) . '/../..') . '/media/uploads/images/client/'.$rResult['image'];

		$pdf->AddPage();



		$pdf->SetXY(0, 8);
		$pdf->SetFont('DejaVu', '', 23);
		$pdf->Cell(0, 0,$rResult[prod].'  შპს "ჯი ეს იუნიტი"-ს მიერ', 0, 0, 'L');

		$pdf->Cell( 0, 0, $pdf->Image($img_patch, 190, 2, 23), 0, 15, 'R', false );

		$pdf->Line(1, 28, 210, 28);

		if($rResult['image'] !== '0.jpg')
			 $pdf->Cell( 0, 0, $pdf->Image($client_image, 0, 40, 23), 0, 15, 'L', false );

		$fSize = 30;
		$sY    = 29;

		$sizeController = 0;

		switch (utf8_decode($rResult['client'])){
			case (strlen(utf8_decode($rResult['client'])) <= 20) :
				$fSize = 51;
				$sY    = 32;
				break;
			case (strlen(utf8_decode($rResult['client'])) <= 30) :
				$fSize = 37;
				$sY    = 30;
				break;
			case (strlen(utf8_decode($rResult['client'])) <= 40) :
				$fSize = 30;
				$sY    = 30;
				break;
			case (strlen(utf8_decode($rResult['client'])) <= 60) :
				$fSize = 30;
				$sY    = 28;
				break;
			default:
				$fSize = 25;
				$sY    = 27;
			 break;
		}


		switch (utf8_decode($rResult['object'])){
			case (strlen(utf8_decode($rResult['object'])) <= 20) :
				$ofSize = 45;
				$osY    = 54;
				break;
			case (strlen(utf8_decode($rResult['object'])) <= 30) :
				$ofSize = 30;
				$osY    = 52;
				break;
			case (strlen(utf8_decode($rResult['object'])) <= 40) :
				$ofSize = 26;
				$osY    = 50;
				break;
			case (strlen(utf8_decode($rResult['object'])) <= 60) :
				$ofSize = 23;
				$osY    = 48;
				break;

			default:
				$ofSize = 19;
				$osY    = 47;
				break;

				break;
		}


		$pdf->SetXY(25, $sY);
		$pdf->SetFont('DejaVu', '', $fSize);
		$pdf->MultiCell(188, 10, $rResult['client'], 0, 'R');

		$pdf->SetXY(25, $osY);
		$pdf->SetFont('DejaVu', '', $ofSize);
		$pdf->MultiCell(188, 10, '(' . $rResult['object'] . ')', 0, 'R');


		$pdf->SetXY(0, 15);
		$pdf->SetFont('DejaVu', '', 23);
		$pdf->MultiCell(188, 10, 'ბარათი: '.$rResult['latter'], 0, 'L');

		$pdf->SetXY(73, 15);
		$pdf->SetFont('DejaVu', '', 23);
		$pdf->MultiCell(188, 10, 'თარიღი:' . $rResult['job_end'], 0, 'L');

		$pdf->Line(1, 67, 210, 67);

		$pdf->SetXY(0, 72);
		$pdf->SetFont('DejaVu', '', 23);
		$pdf->Cell(0, 0, 'Tel: 2 555 118 www.gsunity.ge service@gsunity.ge', 0, 0, 'C');

		$pdf->Output();
		break;
	default:
		echo 'Action is Null';
}


/* ******************************
 *	Cartridge Functions
* ******************************
*/

function GetBarCode($id)
{
	$res = mysql_query("SELECT	`code`
						FROM	`barcodes`
						WHERE	`id` = '$id'");

	$row = mysql_fetch_assoc($res);
	return $row['code'];
}

function GetClientInfo($code)
{
	$res = mysql_query("SELECT 	`client`.`name` AS `name`,
						`client`.`image` AS `image`,
						if(SUBSTR('$code', 1, 1)=3,'პრინტერი შეკეთებულია' ,'კარტრიჯი აღდგენილია') as prod,
	                    (SELECT `barcodes`.`serial_number` FROM `barcodes` WHERE `barcodes`.`code` = '$code' LIMIT 1) AS `code`,   
						(SELECT `production`.`name` FROM `production` WHERE `production`.`id` = SUBSTR('$code', 6, 4)) AS `cartridge`
						FROM 	`client`
						WHERE 	`client`.`id` = SUBSTR('$code', 2, 4)
			");

	$row 	= mysql_fetch_assoc($res);
	$array 	= array(
		"name" 		=> $row['name'],
		"image"		=> $row['image'],
		"cartridge" => $row['cartridge'],
	    "code" => $row['code'],
		"prod" => $row['prod']
	);
	return $array;
}
?>